﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;

namespace WebApplication1
{
    public partial class MainSite : System.Web.UI.MasterPage
    {
        sv_QuanLyNhaSach.AdapterClient service = new sv_QuanLyNhaSach.AdapterClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserName"] != null)
                {
                    Panel1.Visible = false;
                    Panel2.Visible = true;
                    UserName.Text = Session["UserName"].ToString();
                }
                else if (Request.Cookies["Login"] != null)
                {
                    Panel1.Visible = false;
                    Panel2.Visible = true;
                    UserName.Text = Request.Cookies["Login"]["UserName"].ToString();
                    Session["UserName"] = Request.Cookies["Login"]["UserName"].ToString();
                }
            }
            LoadDDL();
        }
        private void LoadDDL()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select * from dausach")));
            
            if (ds.Tables.Count > 0)
            {
                if (!IsPostBack)
                {
                    DropDownList1.DataSource = ds.Tables[0];
                    DropDownList1.DataTextField = "tendausach";
                    DropDownList1.DataValueField = "kihieu";
                    DropDownList1.DataBind();
                }
            }
            for (int i = DropDownList1.Items.Count; i < 0; i++)
            {

            }
            
        }
            
        protected void LogOut_Click(object sender, EventArgs e)
        {
            Response.Cookies["Login"].Expires = DateTime.Now;
            Session["UserName"] = null;
            Session["PassWord"] = null;
            Session.Clear();
            Response.Redirect("~/TrangChu.aspx");
            Panel1.Visible = true;
            Panel2.Visible = false;
        }
        private void TimSach(string ten)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select mamh,tenmh,dongiale,khuyenmai,tacgia,nhasx from mathang where maloai like 'SA'")));
            if (ds.Tables.Count > 0)
            {
                ds.Tables[0].Columns.Add("tensach", typeof(string));
                DataRow dr;
                DataTable tbl;
                if (Session["TimSach"] == null)
                {
                    tbl = new DataTable();
                    tbl.Columns.Add("MaMH", typeof(string));
                    tbl.Columns.Add("TenMH", typeof(string));
                    tbl.Columns.Add("TacGia", typeof(string));
                    tbl.Columns.Add("NhaSX", typeof(string));
                    tbl.Columns.Add("DonGia", typeof(double));
                    tbl.Columns.Add("KhuyenMai", typeof(float));
                }
                else
                    tbl = (DataTable)Session["TimSach"];
                if (DropDownList1.SelectedValue.ToString() != "NA")
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        ds.Tables[0].Rows[i]["tensach"] = RemoveSign4VietnameseString(ds.Tables[0].Rows[i]["tenmh"].ToString().ToLower());
                        if (ds.Tables[0].Rows[i]["tensach"].ToString().Contains(RemoveSign4VietnameseString(ten.ToLower())) == true && ds.Tables[0].Rows[i]["MaMH"].ToString().ToLower().Contains(DropDownList1.SelectedValue.ToLower().Trim()) == true)
                        {
                            dr = tbl.NewRow();
                            dr["MaMH"] = ds.Tables[0].Rows[i]["MaMH"];
                            dr["TenMH"] = ds.Tables[0].Rows[i]["TenMH"];
                            dr["TacGia"] = ds.Tables[0].Rows[i]["TacGia"];
                            dr["NhaSX"] = ds.Tables[0].Rows[i]["NhaSX"];
                            dr["DonGia"] = ds.Tables[0].Rows[i]["DonGiale"];
                            dr["KhuyenMai"] = ds.Tables[0].Rows[i]["KhuyenMai"];
                            tbl.Rows.Add(dr);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        ds.Tables[0].Rows[i]["tensach"] = RemoveSign4VietnameseString(ds.Tables[0].Rows[i]["tenmh"].ToString().ToLower());
                        if (ds.Tables[0].Rows[i]["tensach"].ToString().Contains(RemoveSign4VietnameseString(ten.ToLower())) == true)
                        {
                            dr = tbl.NewRow();
                            dr["MaMH"] = ds.Tables[0].Rows[i]["MaMH"];
                            dr["TenMH"] = ds.Tables[0].Rows[i]["TenMH"];
                            dr["TacGia"] = ds.Tables[0].Rows[i]["TacGia"];
                            dr["NhaSX"] = ds.Tables[0].Rows[i]["NhaSX"];
                            dr["DonGia"] = ds.Tables[0].Rows[i]["DonGiale"];
                            dr["KhuyenMai"] = ds.Tables[0].Rows[i]["KhuyenMai"];
                            tbl.Rows.Add(dr);
                        }
                    }
                }
                Session["TimSach"] = tbl;
            }
        }
        private readonly string[] VietnameseSigns = new string[]{
                                                                 "aAeEoOuUiIdDyY",
                                                                 "áàạảãâấầậẩẫăắằặẳẵ",
                                                                 "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ",
                                                                 "éèẹẻẽêếềệểễ",
                                                                 "ÉÈẸẺẼÊẾỀỆỂỄ",
                                                                 "óòọỏõôốồộổỗơớờợởỡ",
                                                                 "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ",
                                                                 "úùụủũưứừựửữ",
                                                                 "ÚÙỤỦŨƯỨỪỰỬỮ",
                                                                 "íìịỉĩ",
                                                                 "ÍÌỊỈĨ",
                                                                 "đ",
                                                                 "Đ",
                                                                 "ýỳỵỷỹ",
                                                                 "ÝỲỴỶỸ"
                                                                 };
        /// <summary>
        /// Hàm sẽ thay thế các kí tự có dấu thành những kí tự không dấu
        /// </summary>
        /// <param name="str">Nhập vào chuỗi kí tự cần thay thế</param>
        /// <returns>Trả về 1 chuỗi kí tự không dấu</returns>
        private string RemoveSign4VietnameseString(string str)
        {
            //Tiến hành thay thế , lọc bỏ dấu cho chuỗi
            for (int i = 1; i < VietnameseSigns.Length; i++)
            {
                for (int j = 0; j < VietnameseSigns[i].Length; j++)
                    str = str.Replace(VietnameseSigns[i][j], VietnameseSigns[0][i - 1]);
            }
            return str;
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Session["TimSach"] = null;
            TimSach(TextBox1.Text);
            Response.Redirect("KQTimKiem.aspx");
        }
    }
}